package com.niiwoo.civet.account.service.local.openAccount;

import com.niiwoo.civet.account.dao.entity.ActivateAccountOrder;
import com.niiwoo.civet.account.dao.entity.GroupAccountCorrelateOrder;
import com.niiwoo.civet.account.dao.entity.OpenAccountOrder;
import com.niiwoo.civet.account.dto.common.LanMaoGatewayRequestDTO;
import com.niiwoo.civet.account.dto.request.CreateRegisterOrderRequestDTO;
import com.niiwoo.tripod.lanmao.enums.AuthenticateValidTypeEnum;
import com.niiwoo.tripod.lanmao.enums.CredentialsTypeEnum;
import com.niiwoo.tripod.lanmao.enums.GroupAccountCorrelateTypeEnum;
import com.niiwoo.tripod.lanmao.enums.UserLimitTypeEnum;
import com.niiwoo.tripod.lanmao.enums.UserRoleEnum;
import com.niiwoo.tripod.lanmao.request.ActivateAccountRequest;
import com.niiwoo.tripod.lanmao.request.GroupAccountCorrelateRequest;
import com.niiwoo.tripod.lanmao.request.PersonalBindBankCardRegisterRequest;
import com.niiwoo.tripod.lanmao.response.LanMaoGatewayDTO;
import java.math.BigDecimal;
import java.util.Date;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.LocalDate;

public class AccountLanMaoGatewayRequestBuilder {

    static LanMaoGatewayRequestDTO newRequest(LanMaoGatewayDTO lanMaoGatewayDTO) {
        LanMaoGatewayRequestDTO responseDTO = new LanMaoGatewayRequestDTO();
        responseDTO.setAction(lanMaoGatewayDTO.getAction());
        responseDTO.setParams(lanMaoGatewayDTO.getParams());
        return responseDTO;
    }

    /**
     * 个人绑卡注册网关请求参数
     */
    static LanMaoGatewayRequestDTO newPersonalBindBankCardRegisterRequest(Function<PersonalBindBankCardRegisterRequest, LanMaoGatewayDTO> f, OpenAccountOrder order, CreateRegisterOrderRequestDTO requestDTO, boolean hasUnactivatedRecord) {
        PersonalBindBankCardRegisterRequest request = new PersonalBindBankCardRegisterRequest();
        request.setPlatformUserNo(order.getAccountNo());
        request.setRequestNo(order.getRequestNo());
        request.setRealName(order.getRealName());
        request.setIdCardNo(requestDTO.getIdCardNo());
        request.setBankcardNo(requestDTO.getBankCardNo());
        request.setMobile(requestDTO.getBankCardMobileNo());
        request.setIdCardType(CredentialsTypeEnum.PRC_ID);
        request.setUserRole(UserRoleEnum.enumOf(order.getUserRole()));
        request.setCheckType(AuthenticateValidTypeEnum.LIMIT);
        // 没有待激活的账户则校验ID唯一性
        if (!hasUnactivatedRecord) {
            request.setUserLimitType(UserLimitTypeEnum.ID_CARD_NO_UNIQUE);
        }
        request.setAuthList(order.getAuthList());
        request.setGroupAccount("TRUE"); // 默认开通集团账户
        request.setTimestamp(new Date());
        if (StringUtils.isNotBlank(requestDTO.getRedirectUrl())) {
            request.setNiiwooRedirectUrl(requestDTO.getRedirectUrl());
        }
        request.setFailTime(getFixFailTime());
        request.setAmount(getFixLimitAmount());
        return newRequest(f.apply(request));
    }

    /**
     * 个人绑卡注册/会员激活/集团账户关联/用户授权接口中涉及授权期限和授权金额参数如下: 借款人:     授权期限99年，授权金额100万元； 投资人:
     * 授权期限99年，授权金额100万元。
     */
    static Date getFixFailTime() {
        return new LocalDate().plusYears(99).toDate();
    }

    static BigDecimal getFixLimitAmount() {
        return new BigDecimal("1000000.00");
    }


    /**
     * 集团账户关联网关请求参数
     */
    static LanMaoGatewayRequestDTO newGroupAccountCorrelateRequest(Function<GroupAccountCorrelateRequest, LanMaoGatewayDTO> f, GroupAccountCorrelateOrder order, GroupAccountCorrelateTypeEnum correlateType, String redirectUrl) {
        GroupAccountCorrelateRequest request = new GroupAccountCorrelateRequest();
        request.setRequestNo(order.getRequestNo());
        request.setPlatformUserNo(order.getAccountNo());
        request.setGroupAccountNo(order.getGroupAccountNo());
        request.setType(correlateType);
        request.setUserRole(UserRoleEnum.enumOf(order.getUserRole()));
        request.setAuthList(order.getAuthList());
        request.setTimestamp(new Date());
        if (StringUtils.isNotBlank(redirectUrl)) {
            request.setNiiwooRedirectUrl(redirectUrl);
        }
        request.setFailTime(getFixFailTime());
        request.setAmount(getFixLimitAmount());
        return newRequest(f.apply(request));
    }

    static LanMaoGatewayRequestDTO newActivateAccountRequest(Function<ActivateAccountRequest, LanMaoGatewayDTO> f, ActivateAccountOrder order, String redirectUrl) {
        ActivateAccountRequest request = new ActivateAccountRequest();
        request.setRequestNo(order.getRequestNo());
        request.setPlatformUserNo(order.getAccountNo());
        if (order.getUserRole().equals(UserRoleEnum.BORROWERS.getValue()) || order.getUserRole().equals(UserRoleEnum.INVESTOR.getValue())) {
            request.setGroupAccount("TRUE");
        }
        request.setCheckType(AuthenticateValidTypeEnum.LIMIT);
        request.setTimestamp(new Date());
        request.setAuthList(order.getAuthList());
        if (StringUtils.isNotBlank(redirectUrl)) {
            request.setNiiwooRedirectUrl(redirectUrl);
        }
        request.setFailTime(getFixFailTime());
        request.setAmount(getFixLimitAmount());
        return newRequest(f.apply(request));
    }

}
